<?php
class vB_ProfileBlock_Btstats extends vB_ProfileBlock
{
	var $template_name = 'memberinfo_block_btstats';

	function confirm_empty_wrap()
	{
		return false;
	}

	function confirm_display()
	{
		return true;
	}

	function prepare_output($id = '', $options = array())
	{
		// Upload count
		$counttorrent = $this->registry->db->query_first_slave("
			SELECT COUNT(*) AS torrents
			FROM " . TABLE_PREFIX . "filedata AS filedata
			INNER JOIN " . TABLE_PREFIX . "torrent AS torrent ON (torrent.torrentid = filedata.filedataid)
			WHERE filedata.userid = {$this->profile->userinfo['userid']}
		");

		$this->block_data['uploadcount'] = intval($counttorrent['torrents']);

		// Complete count
		$countcomplete = $this->registry->db->query_first_slave("
			SELECT COUNT(*) AS complete
			FROM " . TABLE_PREFIX . "torrentcomplete
			WHERE userid = {$this->profile->userinfo['userid']}
		");

		$this->block_data['completecount'] = intval($counttorrent['complete']);

		// Peers
		$peers = $this->registry->db->query_read_slave("
			SELECT thread.threadid, thread.title AS threadtitle, torrentpeers.`left`
			FROM " . TABLE_PREFIX . "post AS post
			INNER JOIN " . TABLE_PREFIX . "thread AS thread ON (thread.threadid = post.threadid)
			INNER JOIN " . TABLE_PREFIX . "attachment AS a ON (a.contentid = post.postid AND a.state = 'visible')
			INNER JOIN " . TABLE_PREFIX . "torrent AS torrent ON (torrent.torrentid = a.filedataid)
			INNER JOIN " . TABLE_PREFIX . "torrentpeers AS torrentpeers ON (torrentpeers.torrentid = torrent.torrentid)
			WHERE post.visible = 1
				AND thread.visible = 1
				AND a.contenttypeid = 1
				AND torrentpeers.userid = {$this->profile->userinfo['userid']}
		");

		$i = 0;
		$j = 0;
		$this->block_data['seeding'] = '';
		$this->block_data['leeching'] = '';
		while ($peer = $this->registry->db->fetch_array($peers))
		{
			$templater = vB_Template::create('memberinfo_block_btbit');
				$templater->register('thread', $peer);
			$html = $templater->render();
			if ($peer['left'])
			{
				$j ++;
				if ($this->block_data['leeching'] == '')
				{
					$this->block_data['leeching'] .= $html;
				}
				else
				{
					$this->block_data['leeching'] .= ', ' . $html;
				}
			}
			else
			{
				$i ++;
				if ($this->block_data['seeding'] == '')
				{
					$this->block_data['seeding'] .= $html;
				}
				else
				{
					$this->block_data['seeding'] .= ', ' . $html;
				}
			}
		}
		$this->registry->db->free_result($peers);
		$this->block_data['seedingcount'] = $i;
		$this->block_data['leechingcount'] = $j;
	}
}
